package com.bra.ui.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.bra.R;
import com.bra.bean.ArticleBean;
import com.bra.request.UrlConstants;
import com.bra.ui.adapter.ArticleList_adapter;
import com.bra.ui.base.BaseActivity;
import com.bra.utils.CommonUtils;
import com.bra.utils.DataCleanManager;
import com.bra.utils.ToastUtil;

/**
 * 资讯列表界面
 * 
 * @author lenovo
 *
 */
public class ArticleListActivity extends BaseActivity {
	private ImageView activity_back;
	private TextView activity_title;
	private ListView zixun_lv;
	private ArticleList_adapter adapter;
	private String ZixunUrl = UrlConstants.IP;
	private RequestQueue mRequestQueue;
	private ArticleBean  Zixun;
	private List<ArticleBean>ZixunS;

	@Override
	protected View onCreateView(LayoutInflater inflater,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.activity_articlelist, null);
	}

	@Override
	protected void onViewCreated(Bundle savedInstanceState) {
		mRequestQueue = Volley.newRequestQueue(this);
		activity_title = (TextView) findViewById(R.id.activity_title);
		activity_back = (ImageView) findViewById(R.id.activity_back);
		activity_title.setText("资讯");
		zixun_lv = (ListView) findViewById(R.id.zixun_lv);
		httpRequestZixunInfo(ArticleListActivity.this, ZixunUrl, RequestParams());
	}

	@Override
	protected void setListener() {
		activity_back.setOnClickListener(click);
		zixun_lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {				
				Bundle bundle=new Bundle();
				bundle.putString("id", ZixunS.get(position).getId());
				goActivity(ArticleDetailsActivity.class,bundle);			
				
			}
		});
	}

	private String cache;
	private View.OnClickListener click = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.activity_back:
				finish();
				break;

			default:
				break;
			}

		}
	};

	private void httpRequestZixunInfo(Context context, String url,
			final HashMap<String, String> params) {
		StringRequest req = new StringRequest(Request.Method.POST, url,
				new Response.Listener<String>() {
					@Override
					public void onResponse(String response) {
						try {	
							// System.out.println(response.toString() + "===");
							ZixunS=new ArrayList<ArticleBean>();
							JSONObject object=new JSONObject(response.toString());
							   JSONArray  data=object.getJSONArray("data");
							   for(int i=0;i<data.length();i++){
								   JSONObject object_data=data.getJSONObject(i);
								   String id	   =object_data.getString("id");
							       String title	   =object_data.getString("title");
							       String info	   =object_data.getString("description");
							       String imageurl	   =object_data.getString("imageSrc");
							  //     String pl_count	   =object_data.getString("title");
							   	   Zixun=new ArticleBean();
							       Zixun.setTitle(title);
							       Zixun.setInfo(info);
							       Zixun.setImageUrl(imageurl);
							       Zixun.setId(id);
							     //  Zixun.setPl_count(pl_count);
							       ZixunS.add(Zixun) ;
							   }										
							   if (adapter == null) {
									adapter = new ArticleList_adapter(ArticleListActivity.this, ZixunS);
									zixun_lv.setAdapter(adapter);
								}else{
									adapter.notifyDataSetChanged();
								}
								
							return;
						} catch (Exception e) {
							e.printStackTrace();
						}
						Toast.makeText(ArticleListActivity.this, "数据异常，请检查网络", 0)
								.show();
					}
				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError volleyError) {
						Toast.makeText(ArticleListActivity.this, "访问失败，请检查网络", 0)
								.show();
					}
				}) {
			@Override
			protected Map<String, String> getParams() throws AuthFailureError {
				Map<String, String> map = params;
				return map;
			}
		};
		mRequestQueue.add(req);
	}

	private HashMap<String, String> RequestParams() {
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("userId", "");
		map.put("equipment", "android");
		map.put("requestType", "List_Article_Api");
		map.put("token", "");
		map.put("pageNo", "0");
		return map;
	}
}